﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Colietsfortoolholderswithtaperratio1_101
    {
        public static void _Colietsfortoolholderswithtaperratio1_101(Dictionary<string, Object> data)//A型弹簧夹头
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层



            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = Convert.ToDouble(data["scale"]); //放大系数
            //double scale = 5;

            double d1 = Convert.ToDouble(data["d1"]) * scale;
            double d2 = Convert.ToDouble(data["d2"]) * scale;
            double d3 = Convert.ToDouble(data["d3"]) * scale;
            double d4 = Convert.ToDouble(data["d4"]) * scale;
            double l1 = Convert.ToDouble(data["l1"]) * scale;
            double l2 = Convert.ToDouble(data["l2"]) * scale;
            double l4 = Convert.ToDouble(data["l4"]) * scale;
            double l5 = Convert.ToDouble(data["l5"]) * scale;
            double l6 = Convert.ToDouble(data["l6"]) * scale;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X, v1.Y + d3 / 2 + 2);
            Vector2 v3 = new Vector2(v1.X, v1.Y - d3 / 2 - 2);
            Vector2 v4 = fun.Polar(v1, 80 * pi / 180, d1 / 2);
            Vector2 v5 = new Vector2(v4.X, v1.Y + d3 / 2);
            Vector2 v7 = fun.Polar(v1, 40 * pi / 180, d1 / 2);
            Vector2 v6 = fun.Polar(v7, 30 * pi / 180, v5.Y - v4.Y);
            Vector2 v8 = new Vector2(v4.X, v1.Y + d3 / 2 - (l4 - l5) * Math.Tan(30 * pi / 180));
            Vector2 v9 = fun.Polar(v7, 30 * pi / 180, v8.Y - v4.Y);
            Vector2 v10 = new Vector2(v4.X, v8.Y - d3 / 20);
            Vector2 v11 = new Vector2(v10.X - (v10.X - v1.X) * 2, v10.Y);
            Vector2 v12 = new Vector2(v4.X, v4.Y + d3 / 5.263);
            Vector2 v13 = new Vector2(v11.X, v12.Y);

            Vector2 v14 = new Vector2(v1.X + d3 * 2, v1.Y);
            Vector2 v15 = new Vector2(v14.X + l1 + 4, v14.Y);
            Vector2 v16 = new Vector2(v15.X - 2, v15.Y);
            Vector2 v18 = new Vector2(v16.X - l4 - l5, v16.Y + d3 / 2);
            Vector2 v17 = new Vector2(v16.X, v18.Y - (l4 - l5) * Math.Tan(30 * pi / 180));
            Vector2 v19 = new Vector2(v18.X - l5 / 2, v18.Y);
            Vector2 v20 = new Vector2(v19.X, v19.Y - d3 / 17.969);
            Vector2 v21 = new Vector2(v20.X - l5 / 2, v20.Y);
            Vector2 v22 = new Vector2(v21.X, v21.Y - d3 / 17.969);
            Vector2 v23 = new Vector2(v21.X - l2 / 2.42, v22.Y);
            Vector2 v25 = new Vector2(v21.X - l2, v16.Y + d2 / 2);
            Vector2 v24 = new Vector2(v23.X, v25.Y + (v23.X - v25.X) * Math.Tan(2.866 * pi / 180));
            Vector2 v26 = new Vector2(v16.X - l1, v25.Y - (l1 - l2 - l4) * Math.Tan(2.866 * pi / 180));
            Vector2 v27 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v28 = new Vector2(v18.X, v18.Y * -1);
            Vector2 v29 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v30 = new Vector2(v20.X, v20.Y * -1);
            Vector2 v31 = new Vector2(v21.X, v21.Y * -1);
            Vector2 v32 = new Vector2(v22.X, v22.Y * -1);
            Vector2 v33 = new Vector2(v23.X, v23.Y * -1);
            Vector2 v34 = new Vector2(v24.X, v24.Y * -1);
            Vector2 v64 = new Vector2(v25.X, v25.Y * -1);
            Vector2 v35 = new Vector2(v26.X, v26.Y * -1);
            Vector2 v37 = new Vector2(v18.X, v6.Y);
            Vector2 v36 = new Vector2(v16.X, v17.Y - (v18.Y - v37.Y));
            Vector2 v38 = new Vector2(v19.X, v37.Y);
            Vector2 v39 = new Vector2(v21.X, v38.Y - (v20.X - v21.X) * Math.Tan(2.866 * pi / 180));
            Vector2 v40 = new Vector2(v23.X, v39.Y - (v22.X - v23.X) * Math.Tan(2.866 * pi / 180));
            Vector2 v41 = new Vector2(v38.X - l1 / 1.5, v38.Y - (l1 / 1.5) * Math.Tan(2.866 * pi / 180));
            Vector2 v42 = fun.Polar(v41, 177 * pi / 180, l1 / 12.282);
            Vector2 v49 = new Vector2(v36.X, v36.Y - d3 / 35.431);
            Vector2 v48 = new Vector2(v37.X, v37.Y - d3 / 35.431);
            Vector2 v47 = new Vector2(v38.X, v48.Y);
            Vector2 v46 = new Vector2(v39.X, v39.Y - d3 / 35.431);
            Vector2 v45 = new Vector2(v40.X, v40.Y - d3 / 35.431);
            Vector2 v44 = new Vector2(v41.X, v41.Y - d3 / 35.431);
            Vector2 v43 = fun.Polar(v44, -177 * pi / 180, l1 / 12.282);
            Vector2 v50 = new Vector2(v49.X, v49.Y * -1);
            Vector2 v51 = new Vector2(v48.X, v48.Y * -1);
            Vector2 v52 = new Vector2(v47.X, v47.Y * -1);
            Vector2 v53 = new Vector2(v46.X, v46.Y * -1);
            Vector2 v54 = new Vector2(v45.X, v45.Y * -1);
            Vector2 v55 = new Vector2(v44.X, v44.Y * -1);
            Vector2 v56 = new Vector2(v43.X, v43.Y * -1);
            Vector2 v57 = new Vector2(v42.X, v42.Y * -1);
            Vector2 v58 = new Vector2(v41.X, v41.Y * -1);
            Vector2 v59 = new Vector2(v40.X, v40.Y * -1);
            Vector2 v60 = new Vector2(v39.X, v39.Y * -1);
            Vector2 v61 = new Vector2(v38.X, v38.Y * -1);
            Vector2 v62 = new Vector2(v37.X, v37.Y * -1);
            Vector2 v63 = new Vector2(v36.X, v36.Y * -1);
            Vector2 v65 = new Vector2(v16.X, v16.Y + d1 / 2);
            Vector2 v66 = new Vector2(v16.X - l1 / 2, v65.Y);
            Vector2 v67 = new Vector2(v26.X + l6, v16.Y + d4 / 2);
            Vector2 v68 = new Vector2(v26.X, v67.Y);
            Vector2 v69 = new Vector2(v65.X, v65.Y * -1);
            Vector2 v70 = new Vector2(v66.X, v66.Y * -1);
            Vector2 v71 = new Vector2(v67.X, v67.Y * -1);
            Vector2 v72 = new Vector2(v68.X, v68.Y * -1);




            //作图
            Line line1 = new Line(v3, v2);
            line1.Layer = center;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes[0].Bulge = Math.Tan(-58 * pi / 180 / 4);
            line2.Vertexes[2].Bulge = Math.Tan(58 * pi / 180 / 4);
            line2.Vertexes[4].Bulge = Math.Tan(-40 * pi / 180 / 4);
            fun.PolyFill(dxf, line2, 0.5, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            line3.Vertexes.Add(new LwPolylineVertex(v12));
            line3.Vertexes.Add(new LwPolylineVertex(v13));
            fun.PolyFill(dxf, line3, 0.5, contour);

            LwPolyline[] linepoarray1 = new LwPolyline[100];
            linepoarray1 = fun.arrayrpolyline(line2, 6);
            for (int i = 1; i <= 6; i++)
            {
                fun.PolyFill(dxf, linepoarray1[i], 0.5, contour);
            }
            ;

            LwPolyline[] linepoarray2 = new LwPolyline[100];
            linepoarray2 = fun.arrayrpolyline(line3, 6);
            for (int i = 1; i <= 6; i++)
            {
                fun.PolyFill(dxf, linepoarray2[i], 0.5, contour);
            }
            ;

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v37));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v27));
            line4.Vertexes.Add(new LwPolylineVertex(v28));
            line4.Vertexes.Add(new LwPolylineVertex(v62));
            fun.PolyFill(dxf, line4, 0.5, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v18));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes.Add(new LwPolylineVertex(v38));
            line5.Vertexes.Add(new LwPolylineVertex(v39));
            line5.Vertexes.Add(new LwPolylineVertex(v21));
            line5.Vertexes.Add(new LwPolylineVertex(v20));
            fun.PolyFill(dxf, line5, 0.5, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v28));
            line6.Vertexes.Add(new LwPolylineVertex(v29));
            line6.Vertexes.Add(new LwPolylineVertex(v61));
            line6.Vertexes.Add(new LwPolylineVertex(v60));
            line6.Vertexes.Add(new LwPolylineVertex(v31));
            line6.Vertexes.Add(new LwPolylineVertex(v30));
            fun.PolyFill(dxf, line6, 0.5, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v22));
            line7.Vertexes.Add(new LwPolylineVertex(v23));
            fun.PolyFill(dxf, line7, 0.5, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v32));
            line8.Vertexes.Add(new LwPolylineVertex(v33));
            fun.PolyFill(dxf, line8, 0.5, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v40));
            line9.Vertexes.Add(new LwPolylineVertex(v24));
            line9.Vertexes.Add(new LwPolylineVertex(v25));
            line9.Vertexes.Add(new LwPolylineVertex(v26));
            line9.Vertexes.Add(new LwPolylineVertex(v35));
            line9.Vertexes.Add(new LwPolylineVertex(v64));
            line9.Vertexes.Add(new LwPolylineVertex(v34));
            line9.Vertexes.Add(new LwPolylineVertex(v59));
            fun.PolyFill(dxf, line9, 0.5, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v36));
            line10.Vertexes.Add(new LwPolylineVertex(v37));
            line10.Vertexes.Add(new LwPolylineVertex(v38));
            line10.Vertexes.Add(new LwPolylineVertex(v41));
            line10.Vertexes.Add(new LwPolylineVertex(v42));
            line10.Vertexes.Add(new LwPolylineVertex(v43));
            line10.Vertexes.Add(new LwPolylineVertex(v44));
            line10.Vertexes.Add(new LwPolylineVertex(v47));
            line10.Vertexes.Add(new LwPolylineVertex(v48));
            line10.Vertexes.Add(new LwPolylineVertex(v49));
            line10.Vertexes[3].Bulge = Math.Tan(70 * pi / 180 / 4);
            line10.Vertexes[4].Bulge = Math.Tan(110 * pi / 180 / 4);
            line10.Vertexes[5].Bulge = Math.Tan(44 * pi / 180 / 4);
            fun.PolyFill(dxf, line10, 0.5, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v63));
            line11.Vertexes.Add(new LwPolylineVertex(v62));
            line11.Vertexes.Add(new LwPolylineVertex(v61));
            line11.Vertexes.Add(new LwPolylineVertex(v58));
            line11.Vertexes.Add(new LwPolylineVertex(v57));
            line11.Vertexes.Add(new LwPolylineVertex(v56));
            line11.Vertexes.Add(new LwPolylineVertex(v55));
            line11.Vertexes.Add(new LwPolylineVertex(v52));
            line11.Vertexes.Add(new LwPolylineVertex(v51));
            line11.Vertexes.Add(new LwPolylineVertex(v50));
            line11.Vertexes[3].Bulge = Math.Tan(-70 * pi / 180 / 4);
            line11.Vertexes[4].Bulge = Math.Tan(-110 * pi / 180 / 4);
            line11.Vertexes[5].Bulge = Math.Tan(-44 * pi / 180 / 4);
            fun.PolyFill(dxf, line11, 0.5, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v48));
            line12.Vertexes.Add(new LwPolylineVertex(v51));
            line12.Vertexes.Add(new LwPolylineVertex(v52));
            line12.Vertexes.Add(new LwPolylineVertex(v47));
            line12.Vertexes.Add(new LwPolylineVertex(v46));
            line12.Vertexes.Add(new LwPolylineVertex(v53));
            line12.Vertexes.Add(new LwPolylineVertex(v54));
            line12.Vertexes.Add(new LwPolylineVertex(v45));
            fun.PolyFill(dxf, line12, 0.5, contour);

            Line line13 = new Line(v68, v67);
            line13.Layer = refin;
            line13.Linetype = Linetype.DashDot;
            line13.LinetypeScale = 3;
            dxf.AddEntity(line13);

            Line line14 = new Line(v71, v67);
            line14.Layer = refin;
            line14.Linetype = Linetype.DashDot;
            line14.LinetypeScale = 3;
            dxf.AddEntity(line14);

            Line line15 = new Line(v71, v72);
            line15.Layer = refin;
            line15.Linetype = Linetype.DashDot;
            line15.LinetypeScale = 3;
            dxf.AddEntity(line15);

            Line line16 = new Line(v71, v70);
            line16.Layer = refin;
            line16.Linetype = Linetype.DashDot;
            line16.LinetypeScale = 3;
            dxf.AddEntity(line16);

            Line line17 = new Line(v66, v70);
            line17.Layer = refin;
            line17.Linetype = Linetype.DashDot;
            line17.LinetypeScale = 3;
            dxf.AddEntity(line17);

            Line line18 = new Line(v66, v67);
            line18.Layer = refin;
            line18.Linetype = Linetype.DashDot;
            line18.LinetypeScale = 3;
            dxf.AddEntity(line18);

            Line line19 = new Line(v66, v65);
            line19.Layer = refin;
            line19.Linetype = Linetype.DashDot;
            line19.LinetypeScale = 3;
            dxf.AddEntity(line19);

            Line line20 = new Line(v69, v70);
            line20.Layer = refin;
            line20.Linetype = Linetype.DashDot;
            line20.LinetypeScale = 3;
            dxf.AddEntity(line20);



            //标注

            double height = 3.5;
            double arrowsize = height;
            double exline = height / 2;
            double offset = 10;




            fun.dimrot1(dxf, v35, v27, 180, offset * 3 + Math.Abs(v35.Y - v27.Y) / 2 + d3 / 2 + v35.Y, 1, height, arrowsize, exline, "l1=" + l1/scale, "", "");
            fun.dimrot(dxf, v64, v31, 180, offset * 2 + Math.Abs(v64.Y - v31.Y) / 2 + d3 / 2 + v64.Y, height, arrowsize, exline, "l2=" + l2/scale, "", "");

            fun.dimrot1(dxf, v27, v31, 180, offset * 2 + Math.Abs(v27.Y - v31.Y) / 2 + d3 / 2 + v31.Y, 1, height, arrowsize, exline, "l4=" + l4 / scale, "", "");
            fun.dimrot(dxf, v28, v31, 180, offset + Math.Abs(v28.Y - v31.Y) / 2 + d3 / 2 + v28.Y, height, arrowsize, exline, "l5=" + l5 / scale, "", "");
            fun.dimrot(dxf, v35, v71, 180, offset + Math.Abs(v35.Y - v71.Y) / 2 + d3 / 2 + v35.Y, height, arrowsize, exline, "l6=" + l6 / scale, "", "");


            fun.dimrot1(dxf, v65, v69, -90, offset  , 1, height, arrowsize, exline, "d1=%%c" + d1 / scale, "", "");
            fun.dimrot1(dxf, v25, v64, 90, 0, 1, height, arrowsize, exline, "d2=%%c" + d2 / scale, "", "");
            fun.dimrot1(dxf, v18, v28, -90, offset*2+v27.X-v18.X, 1, height, arrowsize, exline, "d3=%%c" + d3 / scale, "", "");
            fun.dimrot1(dxf, v68, v72, 90, offset, 1, height, arrowsize, exline, "d4=%%c" + d4 / scale, "", "");


            fun.jtbz(dxf, ((v35 + v34) / 2+v34)/2, -120, offset, offset, "1:10", height, height);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            fun.SetLayer(dxf); fun.SetByLayer(dxf);



            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }
    }
}

